/*
 * BanBu_MediaView
 * @description 本文件提供chatcell里面的图片显示控件（UIImageView）
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供chatcell里面的图片显示控件（UIImageView）
 */

#import "BanBu_MediaView.h"
#import "AppDataManager.h"
#import "AppCommunicationManager.h"
#import "SCGIFViewController.h"
#import "BanBu_ToseeGifViewController.h"
#import "BanBu_MWNavigationController.h"
#import "UIViewController+Animation.h"
@interface BanBu_MediaView (fuck)

@end

@implementation BanBu_MediaView

@synthesize showButton = _showButton;
@synthesize status = _status;
@synthesize showPhotos=_showPhotos;
@synthesize mediaPath = _mediaPath;
@synthesize gifView=_gifView;
@synthesize imagePath;
-(void)sendMessageTochat{
    
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag=101;
        button.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        self.showButton = button;
        [self addSubview:button];
        
        _status = 3;
        
        _showPhotos=[[NSMutableArray alloc]initWithCapacity:10];
        
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"file-loading.png"]];
        
        UITapGestureRecognizer *taptoseeGif=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Gif)];
        
        taptoseeGif.numberOfTapsRequired=1;
        
        _gifView.userInteractionEnabled=YES;
        [_gifView addGestureRecognizer:taptoseeGif];
        
        [taptoseeGif release];
        
        self.hintLabel = [[[UILabel alloc]initWithFrame:CGRectZero] autorelease];
        self.hintLabel.backgroundColor = [UIColor clearColor];
        self.hintLabel.textAlignment = UITextAlignmentCenter;
        [self addSubview:self.hintLabel];
        self.hintLabel.font = [UIFont systemFontOfSize:12];
    }
    return self;
}

-(void)Gif
{
    if ([self.from isEqual:@"collect"] || [self.from isEqual:@"store"])
    {
        return;
    }
    BanBu_ToseeGifViewController *aSC = [[BanBu_ToseeGifViewController alloc]init];
    
    aSC.gifString = _mediaPath;
    BanBu_MWNavigationController *nc = [[BanBu_MWNavigationController alloc]initWithRootViewController:aSC];
    nc.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    [_appChatController presentViewController:nc animated:YES];
    [aSC release];
    [nc release];
}

- (void)setStatus:(MediaStatus)status
{
    if(_status != status)
    {
        _status = status;
        if(_status != MediaStatusNormal)
        {
            self.showButton.enabled = NO;
        }
        else
        {
            self.showButton.enabled = YES;
        }
    }
    [self layoutSubviews];
}


- (void)layoutSubviews
{
    if(![[_mediaPath pathExtension]isEqualToString:@"gif"])
    {
        self.showButton.frame = self.bounds;
        self.gifView.frame=CGRectZero;
    }
    else
    {
        self.gifView.frame=self.bounds;
        self.showButton.frame=CGRectZero;
    }
    self.hintLabel.frame = CGRectMake(self.showButton.center.x-25, 0, 50, 28);
}


- (void)setMedia:(NSString *)mediaStr
{
    self.hintLabel.hidden = YES;
    self.mediaPath = mediaStr;
    [_showButton setImage:nil forState:UIControlStateNormal];
    
    if(mediaStr == nil)
    {
        return;
    }
    
    [self layoutSubviews];
    
    if (self.snapMsg)
    {
        UIImage *image = [UIImage imageNamed:@"photo_default.png"];
        
        [_showButton setImage:image forState:UIControlStateNormal];
        _showButton.userInteractionEnabled = NO;
        self.hintLabel.frame = CGRectMake(self.center.x-25, 0, 50, 28);
        self.hintLabel.text = NSLocalizedString(@"longPressToSee", nil);
        self.hintLabel.hidden = NO;
        return;
    }
    _showButton.userInteractionEnabled = YES;
    
    
    // 如果是jpg 的就是这种方式
    if([[mediaStr pathExtension]isEqual:@"jpg"]||[[mediaStr pathExtension] isEqual:@"png"])
    {
        UIImage *image = [MyAppDataManager msgImageForImageUrlStr:mediaStr];
        
        [_showButton setImage:image forState:UIControlStateNormal];
    }
    else
    {
        SCGIFImageView *g=(SCGIFImageView*)[self viewWithTag:1000];
        
        if(g)
        {
            [g removeFromSuperview];
        }
        
        SCGIFImageView *gif=[[SCGIFImageView alloc]initWithFrame:self.bounds];
        
        gif.tag=1000;
        
        self.gifView=gif;
        
        [self addSubview:gif];
        
        [gif release];
        gif.gifFile=[AppComManager pathForMsgMedia:mediaStr];
        
        self.gifView.hidden=NO;
        self.gifView.userInteractionEnabled=YES;
        
        UITapGestureRecognizer *taptoseeGif=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Gif)];
        
        [gif addGestureRecognizer:taptoseeGif];
        
        [taptoseeGif release];
    }
}

- (void)setMedia:(NSString *)mediaStr withFrom:(NSString *)from
{
    self.from = from;
    self.hintLabel.hidden = YES;
    self.mediaPath = mediaStr;
    [_showButton setImage:nil forState:UIControlStateNormal];
    
    if(mediaStr == nil)
    {
        return;
    }
    
    [self layoutSubviews];
    
    if (self.snapMsg)
    {
        UIImage *image = [UIImage imageNamed:@"photo_default.png"];
        
        [_showButton setImage:image forState:UIControlStateNormal];
        _showButton.userInteractionEnabled = NO;
        self.hintLabel.frame = CGRectMake(self.center.x-25, 0, 50, 28);
        self.hintLabel.text = NSLocalizedString(@"longPressToSee", nil);
        self.hintLabel.hidden = NO;
        return;
    }
    _showButton.userInteractionEnabled = YES;
    NSString * bimagePath = nil;
    if ([from isEqualToString:@"store"])//这是表情商店的表情
    {
        NSArray * arr = [mediaStr pathComponents];
        NSString * imageName = [mediaStr lastPathComponent];
        NSString * folderName = [arr objectAtIndex:arr.count-2];
        
        bimagePath = [AppComManager pathForEmo:imageName withFolder:folderName];
    }
    else if ([from isEqualToString:@"collect"])//收藏的表情
    {
        bimagePath = [AppComManager pathForCollect:mediaStr];
    }
    else if ([from isEqualToString:@"collectimage"])
    {
        bimagePath = [AppComManager pathForCollectImage:mediaStr];
    }
    else if(mediaStr.length)
    {
        bimagePath = [AppComManager pathForMsgMedia:mediaStr];
    }
    
    self.imagePath = bimagePath;
    // 如果是jpg 的就是这种方式
    if([[mediaStr pathExtension]isEqual:@"jpg"]||[[mediaStr pathExtension] isEqual:@"png"])
    {
        UIImage * image = [UIImage imageWithContentsOfFile:bimagePath];
        
        [_showButton setImage:image forState:UIControlStateNormal];
    }
    else
    {
        SCGIFImageView *g=(SCGIFImageView*)[self viewWithTag:1000];
        
        if(g)
        {
            [g removeFromSuperview];
        }
        
        SCGIFImageView *gif=[[SCGIFImageView alloc]initWithFrame:self.bounds];
        
        gif.tag=1000;
        
        self.gifView=gif;
        
        [self addSubview:gif];
        
        [gif release];
        
        gif.gifFile= self.imagePath;
        
        self.gifView.hidden=NO;
        self.gifView.userInteractionEnabled=YES;
        
        UITapGestureRecognizer *taptoseeGif=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Gif)];
        
        [gif addGestureRecognizer:taptoseeGif];
        
        [taptoseeGif release];
    }
}

- (void)btnAction:(UIButton *)button
{
    //    if ([self.from isEqual:@"collect"] || [self.from isEqual:@"store"])
    //    {
    //        return;
    //    }
    
    if ([self.from isEqual:@"store"])
    {
        return;
    }
    
    NSData * imageData = [MyAppDataManager imageDataForImagePathStr:self.imagePath];
    
    if (imageData == nil || imageData.length<20)
    {
        return;
    }
    MWPhoto *photo=[MWPhoto photoWithImage:[UIImage imageWithData:imageData]];
    
    [_showPhotos removeAllObjects];
    
    [_showPhotos addObject:photo];
    
    // 新建一个数组 存放image
    
    BanBu_ChatToseePic *browser = [[BanBu_ChatToseePic alloc] initWithDelegate:self];
    
    browser.type = DrawTypeChat;
    NSMutableArray *arr=[[[NSMutableArray alloc]initWithCapacity:1]autorelease];
    
    [arr addObject:[UIImage imageWithData:imageData]];
    [arr addObject:_mediaPath];
    browser.shareArr=arr;
    
    [browser setInitialPageIndex:0];
    BanBu_MWNavigationController *nc = [[BanBu_MWNavigationController alloc] initWithRootViewController:browser];
    [self.appChatController presentViewController:nc animated:YES];
    
    [nc release];
    [browser release];
    
    return;
}


- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser
{
    return 1;
}

- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
{
    if (index < _showPhotos.count)
        return [_showPhotos objectAtIndex:index];
    return nil;
}

- (void)dealloc
{
    [_playingView release],_playingView = nil;
    _showButton = nil;
    self.mediaPath = nil;
    [_showPhotos release],_showPhotos = nil;
    self.imagePath = nil;
    [_hintLabel release],_hintLabel = nil;
    
    [super dealloc];
}


@end
